<?php
//$-POST $_GET: 在后端获取前端页面提交数据的全局数组
$userName = trim($_POST['username']);
$pw = trim($_POST['pw']);
$cpm = trim($_POST['cpm']);
$sex = $_POST['sex'];
$fav = implode(",", $_POST['fav']);
$email = $_POST['email'];
//echo "你输入的用户名：".$userName."<br>";
//echo  "你输入的密码:$pw <br>";
//echo  "你确认的密码:$cpm <br>";
//echo "输入的性别是：" ;
////echo "<br>";
//echo  $sex ==1 ? '男': '女';
//
//if($fav == 0){
//    echo "<br>你没有选择爱好";
//}else{
//    echo "<br>选择的爱好: ";
//    $fav1 = implode( ',',$fav);
//    echo $fav1;
//}

//第一步链接数据库

//$conn = mysqli_connect("member", "root", "root", "member");
//
//if(!$conn){
//    die("数据库连接失败");
//}
////第二步： 设置字符集
//mysqli_query($conn,"set names utf8");
include_once "conn.php";
//进行数据的验证
if(!strlen($userName)|| !strlen($pw)){
    echo "<script>alert('用户名和密码必须填写');
     history.back()
</script>";
    exit;

}else{
    if(!preg_match('/^[a-zA-Z0-9_]{3,10}$/', $userName)){
        echo "<script>alert('用户名必须填写，且只能大小写字符和数字下划线组成，长度为3~10个字符');
     history.back()
</script>";
        exit;
    }
}
if($pw <> $cpm){
    echo "<script>alert('密码和确认密码必须相同');
     history.back()
</script>";
    exit;
}else {
    if (!preg_match('/^[a-zA-Z0-9_*]{6,10}$/', $pw)) {
        echo "<script>alert('密码必须填写，且只能大小写字符和数字下划线和*组成，长度为6~10个字符two');
     history.back()
</script>";
        exit;
    }
}
//判断用户名是否重复
$sql ="select * from info where username = '$userName'";
$result = mysqli_query($conn, $sql);//返回一个记录表
$num = mysqli_num_rows($result);
if($num){
    echo "<script>alert('此用户名已被占用，请重新输入');
     history.back()
</script>";
    exit;



}
if(!empty($email)){
    if(!preg_match('/^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)(com|cn|net|org)$/', $email)){
        echo "<script>alert('邮箱格式不正确');
     history.back()
</script>";
        exit;
    }
}
$sql = "insert into info (username, pw, sex, email, fav, createTime) values ('$userName', '".md5($pw)."', '$sex', '$email', '$fav', '".time()."')";
//执行查询语句

$result = mysqli_query($conn, $sql);
if($result){
    echo "<script>alert('数据插入成功');location.href='index.php'</script>";
}else{
    echo "<script>alert('数据插入失败');</script>";
}

